/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.gac.ui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
import org.gac.model.CompositeStep;
import org.gac.model.Step;
import org.netbeans.api.visual.action.ActionFactory;
import org.netbeans.api.visual.action.EditProvider;
import org.netbeans.api.visual.action.PopupMenuProvider;
import org.netbeans.api.visual.border.Border;
import org.netbeans.api.visual.border.BorderFactory;
import org.netbeans.api.visual.layout.LayoutFactory;
import org.netbeans.api.visual.widget.LabelWidget;
import org.netbeans.api.visual.widget.Widget;
import org.openide.util.Exceptions;

/**
 *
 * @author Tomek
 */
public abstract class StepWidgetBase extends Widget
{

    protected BoxScene _scene;
    protected Widget _mainVFlow;
    protected Widget _headerVFlow;
    protected LabelWidget _label;
    protected ButtonWidget _deleteButton;
    protected CompositeStepWidget _parent;
    protected Step _modelStep;

    public StepWidgetBase(BoxScene scene, Step modelStep)
    {
        super(scene);
        _scene = scene;
        _modelStep = modelStep;
        this.setMinimumSize(Dimensions.StepMinSize);

        setLayout(LayoutFactory.createOverlayLayout());

        _mainVFlow = new Widget(scene);
        _mainVFlow.setLayout(Layouts.VerticalLayout);
        this.addChild(_mainVFlow);

        _headerVFlow = new Widget(scene);
        _headerVFlow.setLayout(Layouts.HorizontalLayout);
        this.setBorder(BorderFactory.createLineBorder(1,Color.WHITE));

        _label = new LabelWidget(scene, modelStep.getName());
        _label.setOpaque(false);
        _label.setBackground(new Color(187, 233, 255));
        _label.setMinimumSize(new Dimension(80, 10));
        _label.setAlignment(LabelWidget.Alignment.CENTER);
//        _label.getActions().addAction(ActionFactory.createInplaceEditorAction(
//                new TextFieldInplaceEditor()
//                {
//
//                    public boolean isEnabled(Widget widget)
//                    {
//                        return true;
//                    }
//
//                    public String getText(Widget widget)
//                    {
//                        return ((LabelWidget) widget).getLabel();
//                    }
//
//                    public void setText(Widget widget, String text)
//                    {
//                        ((LabelWidget) widget).setLabel(text);
//                        _modelStep.setName(text);
//                    }
//                }));
//                
        this.getActions().addAction(ActionFactory.createEditAction(new EditProvider()
        {

            public void edit(Widget widget)
            {
                StepWidgetBase.this._scene.stepWidgetSelected(
                        StepWidgetBase.this);
            }
        }));

        this.getActions().addAction(ActionFactory.createMoveAction(
                new MyMoveStrategy(scene),
                new MyMoveProvider(scene)));

//        getActions().addAction(ActionFactory.createSelectAction(
//                new SelectProvider()
//                {
//
//                    public boolean isAimingAllowed(Widget widget,
//                            Point localLocation,
//                            boolean invertSelection)
//                    {
//                        return true;
//                    }
//
//                    public boolean isSelectionAllowed(Widget widget,
//                            Point localLocation,
//                            boolean invertSelection)
//                    {
//                        return true;
//                    }
//
//                    public void select(Widget widget, Point localLocation,
//                            boolean invertSelection)
//                    {
//                        StepWidgetBase.this._scene.stepWidgetSelected(
//                                StepWidgetBase.this);
//                    }
//                }));

//        _deleteButton = new ButtonWidget(scene, "-", new IButtonClickListener()
//        {
//
//            public void Click()
//            {
//                if (StepWidgetBase.this._parent != null)
//                {
//                    _parent.removeInnerStep(StepWidgetBase.this);
//                }
//            }
//        });

        this.getActions().addAction(ActionFactory.createPopupMenuAction(new PopupMenuProvider()
        {

            public JPopupMenu getPopupMenu(Widget widget, Point localLocation)
            {
                JPopupMenu popup = new JPopupMenu();
                JMenuItem remove = new JMenuItem("Remove step");
                remove.addActionListener(new ActionListener()
                {

                    public void actionPerformed(ActionEvent e)
                    {
                        if (StepWidgetBase.this._parent != null)
                        {

                                        ((CompositeStep)StepWidgetBase.this._parent._modelStep).RemoveStep(_modelStep);
                                        //_scene.validate();
                        }
                    }
                });

                popup.add(remove);

                return popup;
            }
        }));


        _headerVFlow.addChild(_label);
        //_headerVFlow.addChild(_deleteButton);
        _mainVFlow.addChild(_headerVFlow);
        //this.setBorder(BorderFactory.createLineBorder(1, Color.LIGHT_GRAY));

    }

    public Step getModelStep()
    {
        return _modelStep;
    }
    private Border defaultBorder;

    public void setSelection(boolean selected)
    {

        if (selected)
        {
            defaultBorder = this.getBorder();
            this.setBorder(BorderFactory.createLineBorder(1,
                    new Color(255, 0, 0)));
        } else
        {
            this.setBorder(defaultBorder);
        }
    }
}
